package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportStaffGradeRpcService;
import com.hunttown.mes.rpc.domain.TransportStaffGradeDTO;
import com.hunttown.mes.rpc.domain.query.TransportStaffGradeDTOQuery;
import com.hunttown.mes.service.TransportStaffGradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportStaffGradeConverts.convert;

/**
 * created by wangjunfu on 2020-01-11 22:20:31
 */
@Service
public class TransportStaffGradeRpcServiceImpl implements TransportStaffGradeRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportStaffGradeRpcServiceImpl.class);

    private final TransportStaffGradeService transportStaffGradeService;

    @Autowired
    public TransportStaffGradeRpcServiceImpl(TransportStaffGradeService transportStaffGradeService) {
        this.transportStaffGradeService = transportStaffGradeService;
    }

    @Override
    public TransportStaffGradeDTO insert(TransportStaffGradeDTO transportStaffGradeDTO) {
        try {
            return convert(transportStaffGradeService.insert(convert(transportStaffGradeDTO)));
        } catch (Exception e) {
            logger.error("TransportStaffGrade create failed.", e);
            throw new RuntimeException("TransportStaffGrade create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportStaffGradeDTO transportStaffGradeDTO) {
        try {
            return transportStaffGradeService.updateInfoById(convert(transportStaffGradeDTO));
        } catch (Exception e) {
            logger.error("TransportStaffGrade update by id failed.", e);
            throw new RuntimeException("TransportStaffGrade update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportStaffGradeDTOQuery transportStaffGradeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportStaffGradeDTOQuery.getW_id() != null && transportStaffGradeDTOQuery.getW_id() > 0) {
                query.put("w_id", transportStaffGradeDTOQuery.getW_id());
            }

            if (transportStaffGradeDTOQuery.getIds() != null && !transportStaffGradeDTOQuery.getIds().equals("")) {
                query.put("ids", transportStaffGradeDTOQuery.getIds());
            }

            if (transportStaffGradeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportStaffGradeDTOQuery.getStopFlag());
            }

            if (transportStaffGradeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportStaffGradeDTOQuery.getDeleteFlag());
            }

            return transportStaffGradeService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportStaffGrade update by query failed.", e);
            throw new RuntimeException("TransportStaffGrade update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportStaffGradeDTO selectById(Integer id) {
        try {
            return convert(transportStaffGradeService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportStaffGrade select by id failed.", e);
            throw new RuntimeException("TransportStaffGrade select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportStaffGradeDTO selectObjByQuery(TransportStaffGradeDTOQuery transportStaffGradeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getGradeName())) {
                query.put("gradeName", transportStaffGradeDTOQuery.getGradeName());
            }

            if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getGradeNameEn())) {
                query.put("gradeNameEn", transportStaffGradeDTOQuery.getGradeNameEn());
            }

            if (transportStaffGradeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportStaffGradeDTOQuery.getStopFlag());
            }

            return convert(transportStaffGradeService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportStaffGrade select obj by query failed.", e);
            throw new RuntimeException("TransportStaffGrade select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportStaffGradeDTO> getListForPage(TransportStaffGradeDTOQuery transportStaffGradeDTOQuery) {
        Query query = Query.page(transportStaffGradeDTOQuery.getPn(), transportStaffGradeDTOQuery.getPageSize());

        if (transportStaffGradeDTOQuery.getId() != null && transportStaffGradeDTOQuery.getId() > 0) {
            query.put("id", transportStaffGradeDTOQuery.getId());
        }

        if (transportStaffGradeDTOQuery.getIds() != null && !transportStaffGradeDTOQuery.getIds().equals("")) {
            query.put("ids", transportStaffGradeDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getLikeName())) {
            query.put("likeName", transportStaffGradeDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getGradeName())) {
            query.put("gradeName", transportStaffGradeDTOQuery.getGradeName());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getGradeNameEn())) {
            query.put("gradeNameEn", transportStaffGradeDTOQuery.getGradeNameEn());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getShowDate())) {
            query.put("showDate", transportStaffGradeDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getStartDate())) {
            query.put("startDate", transportStaffGradeDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getEndDate())) {
            query.put("endDate", transportStaffGradeDTOQuery.getEndDate());
        }

        if (transportStaffGradeDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportStaffGradeDTOQuery.getStopFlag());
        }

        if (StringUtils.isNotBlank(transportStaffGradeDTOQuery.getOrderBy())) {
            query.put("orderBy", transportStaffGradeDTOQuery.getOrderBy());
        }

        List<TransportStaffGradeDTO> list = convert(transportStaffGradeService.selectByQuery(query));
        Page<TransportStaffGradeDTO> page = Page.from(transportStaffGradeDTOQuery.getPn(), transportStaffGradeDTOQuery.getPageSize(), list);
        page.setTotalItems(transportStaffGradeService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportStaffGradeService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportStaffGrade delete failed.", e);
            throw new RuntimeException("TransportStaffGrade delete failed, Please order your code.", e);
        }
    }
}